﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace 二进制文件读写
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        public byte[] getFileBytes(string Filename)
        {
            if (Filename == "")
                return null;
            try
            { FileStream fileStream = new FileStream(Filename, FileMode.Open, FileAccess.Read);
            BinaryReader binaryReader = new BinaryReader(fileStream);
            byte[] fileBytes = binaryReader.ReadBytes((int)fileStream.Length);
            binaryReader.Close();
            fileStream.Close();
            return fileBytes;

            }
            catch { return null; }
         
        }
        public bool WriteFileBytes(string TargetFilename, byte[] fileBytes)
        {
            bool k = true;
            if (TargetFilename != "" && fileBytes.Length != 0)
            {
                try
                {
                    FileStream fileStream = new FileStream(TargetFilename, FileMode.OpenOrCreate, FileAccess.Write);
                    BinaryWriter binaryWriter = new BinaryWriter(fileStream);
                    binaryWriter.Write(fileBytes);
                    binaryWriter.Flush();
                    binaryWriter.Close();
                    fileStream.Close();
                }
                catch { k = false; }
            }
            else
            {
                k = false;
            }
            return k;
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            try { OpenFileDialog openfile = new OpenFileDialog();
            openfile.Filter = "jpg类型图片(*.jpg)|*.jpg|BMP类型图片(*.bmp)|*.bmp";
            if (openfile.ShowDialog() == DialogResult.OK)
            { byte[] picbinary = getFileBytes(openfile.FileName);
            MemoryStream mempicstream = new MemoryStream(picbinary);
            pictureBox1.Image = Image.FromStream(mempicstream);
            mempicstream.Dispose();
            mempicstream.Close();
            }
            }
            catch (Exception m) { MessageBox.Show("读取图片出错,可能的问题是:" + Convert.ToString(m), "提示错误"); }
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            try {if(pictureBox1.Image==null)
            { MessageBox.Show("禁止图片为空时侯另存信息","提示信息");}
            else
            {saveFileDialog1.Filter="jpg类型图片(*.jpg)|*.jpg";
                DialogResult result=saveFileDialog1.ShowDialog();
                if(result==DialogResult.OK)
                {MemoryStream ms=new MemoryStream();
                    Bitmap bm=new Bitmap(pictureBox1.Image);
                    bm.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
                    byte[] bytes = ms.ToArray();
                    WriteFileBytes(saveFileDialog1.FileName, bytes);
                    MessageBox.Show("另存图片成功", "提示");
                    ms.Dispose();
                    ms.Close();
                    bm.Dispose();

                    
                }
            }
}
            catch(Exception m)
            {
            MessageBox.Show("读取图片出错,可能的原因是:"+Convert.ToString(m),"错误提示");}
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            form1.Close();
        }
    }
}
